// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import ElementUI from "element-ui";
import iView from "iview";
import "iview/dist/styles/iview.css";
// import 'element-ui/lib/theme-chalk/index.css'
import App from "./App";
// import axios from "axios";
import router from "./router";
import layer from "../static/js/layer/layer.js";
import "jquery";
import "../src/router/index"

Vue.config.productionTip = false;

require("es6-promise").polyfill();

Vue.use(ElementUI);
Vue.use(iView);

/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  template: "<App/>",
  components: { App }
});

//全局变量
global.tokenURL = URL + "/api/v1/userTokens"; //更换用户验证票
global.URL = "http://dev.bthdtm.com:58040"; //访问服务器url（外网地址）
// global.URL = "http://192.168.0.249:8040"; //访问服务器url（内网地址）
global.loginURL = URL + "/api/v1/userservice/userLogin"; //用户登陆
global.validateCodeInfoURL = URL + "/api/v1/validateCodeInfo"; //获取验证码
global.usersURL = URL + "/api/userservice/v1/users"; //用户
global.rolesURL = URL + "/api/userservice/v1/roles"; //角色
global.grantURL = URL + "/api/userservice/v1/permissions"; //权限
global.sysparamsURL = URL + "/api/userservice/v1/sysparams"; //系统
global.dictionariesURL = URL + "/api/userservice/v1/dictionaries"; //字典
global.logsURL = URL + "/api/userservice/v1/logs"; //日志
global.rolesPurviewTreeURL = URL + "/api/userservice/v1/roles/purviewtree"; //获取指定角色权限树

global.newCount = 1;
//封装存储后台返回的数据
global.appDB = {
  set: function(key, val) {
    localStorage.setItem(key, JSON.stringify(val));
  },
  get: function(key) {
    return JSON.parse(localStorage.getItem(key));
  },
  length: localStorage.length,
  remove: function(key) {
    localStorage.removeItem(key);
  },
  clear: function() {
    localStorage.clear();
  }
};

//
Vue.prototype.indexHeight = function() {
  //计算除head和foot以外
  let windowHeight = $(window).height();
  let indexHeight = windowHeight - 120 + "px";
  $(".index").height(indexHeight);
};
//时定时获取token
Vue.prototype.getToken = function() {
  // console.info(appDB.get("userInfo").token);
  setInterval(() => {
    let timeA = appDB.get("time");
    let timeB = timeA - 1000;
    appDB.set("time", timeB);
    if (appDB.get("time") == 0) {
      //倒计时为零请求换取验证票，然后初始化倒计时
      this.$http
        .patch(tokenURL + "/" + appDB.get("userInfo").loginName, {
          token: appDB.get("userInfo").token
        })
        .then(response => {
          let d = response.data;
          if (d.resultCode == 0) {
            // console.info(d);
            let temp = appDB.get("userInfo");
            temp.token = d.record.token;
            appDB.set("userInfo", temp);
            // console.info(appDB.get("userInfo").token);
          } else {
            //当获取不到token时候，从新登录
            layer.msg(d.msg);
            this.$router.push("/");
            appDB.clear();
          }
        })
        .catch(error => {
          alert("网络错误，不能访问");
        });
      appDB.set("time", 7200000);
    }
    // console.info(appDB.get("time"));
  }, 1000);
}
//页面初始化
Vue.prototype.initPage = function() {
  //验证用户是否登录，没有登录跳转登录页
  if (appDB.get("userInfo") == null) {
    this.$router.push("/");
  }
}
//根据权限值显示对应内容
//全局变量
//菜单权限
// global.menuUsers=false;
// global.menuRoles=false;
// global.menuPermissions=false;
// global.menuSystems=false;
// global.menuDictionaries=false;
// global.menuLogs=false;
//功能权限
// global.usersRead=false;
// global.usersAdd=false;
// global.usersRdit=false;
// global.usersDelete=false;
// global.usersUpdate_password=false;
// global.rolesRead=false;
// global.rolesAdd=false;
// global.rolesEdit=false;
// global.rolesDelete=false;
// global.rolesAuthor=false;
// global.permissionsRead=false;
// global.permissionsAdd=false;
// global.permissionsEdit=false;
// global.permissionsDelete=false;
// global.dictionariesRead=false;
// global.dictionariesAdd=false;
// global.dictionariesEdit=false;
// global.dictionariesDelete=false;
// global.logsRead=false;
global.grantLength = 0;
global.permissions = appDB.get("userInfo").permissions;
//全局方法
Vue.prototype.grantType = function() {
  // console.info(permissions)
  for (grantLength = 0; grantLength < permissions.length; grantLength++) {
    //遍历权限列表
    // console.info(permissions[grantLength]);
    if (permissions[grantLength].permission == "menu:users") {
      //菜单权限
      this.menuUsers = true;
    } else if (permissions[grantLength].permission == "menu:roles") {
      this.menuRoles = true;
    } else if (permissions[grantLength].permission == "menu:permissions") {
      this.menuPermissions = true;
    } else if (permissions[grantLength].permission == "menu:systems") {
      this.menuSystems = true;
    } else if (permissions[grantLength].permission == "menu:dictionaries") {
      this.menuDictionaries = true;
    } else if (permissions[grantLength].permission == "menu:logs") {
      this.menuLogs = true;
    } else if (permissions[grantLength].permission == "users:read") {
      //功能权限
      this.usersRead = true;
    } else if (permissions[grantLength].permission == "users:add") {
      this.usersAdd = true;
    } else if (permissions[grantLength].permission == "users:edit") {
      this.usersEdit = true;
    } else if (permissions[grantLength].permission == "users:delete") {
      this.usersDelete = true;
    } else if (permissions[grantLength].permission == "users:update_password") {
      this.usersUpdate_password = true;
    } else if (permissions[grantLength].permission == "roles:read") {
      this.rolesRead = true;
    } else if (permissions[grantLength].permission == "roles:add") {
      this.rolesAdd = true;
    } else if (permissions[grantLength].permission == "roles:edit") {
      this.rolesEdit = true;
    } else if (permissions[grantLength].permission == "roles:delete") {
      this.rolesDelete = true;
    } else if (permissions[grantLength].permission == "roles:author") {
      this.rolesAuthor = true;
    } else if (permissions[grantLength].permission == "permissions:read") {
      this.permissionsRead = true;
    } else if (permissions[grantLength].permission == "permissions:add") {
      this.permissionsAdd = true;
    } else if (permissions[grantLength].permission == "permissions:edit") {
      this.permissionsEdit = true;
    } else if (permissions[grantLength].permission == "permissions:delete") {
      this.permissionsDelete = true;
    } else if (permissions[grantLength].permission == "dictionaries:read") {
      this.dictionariesRead = true;
    } else if (permissions[grantLength].permission == "dictionaries:add") {
      this.dictionariesAdd = true;
    } else if (permissions[grantLength].permission == "dictionaries:edit") {
      this.dictionariesEdit = true;
    } else if (permissions[grantLength].permission == "dictionaries:delete") {
      this.dictionariesDelete = true;
    } else if (permissions[grantLength].permission == "logs:read") {
      this.logsRead = true;
    } else if (permissions[grantLength].permission == "sysparams:read") {
      this.sysparamsRead = true;
    } else if (permissions[grantLength].permission == "sysparams:add") {
      this.sysparamsAdd = true;
    } else if (permissions[grantLength].permission == "sysparams:edit") {
      this.sysparamsEdit = true;
    } else if (permissions[grantLength].permission == "sysparams:delete") {
      this.sysparamsDelete = true;
    } else {
    }
  }
};
